package zy.blue7.im.core.service;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import zy.blue7.im.common.message.Message;

import java.io.IOException;

/**
 * @Author: blue7
 * @Date: 2022/2/27 20:04
 * @Version 1.0
 * @Description
 */
public interface MessageHandler {

    void handle(Message message, Channel channel) throws IOException;

    default void sendMessage(Message message, Channel channel) {
        /**
         * 如果接收方没下线，就直接发送给他
         */
        if (null != channel) {
            ChannelFuture channelFuture = channel.writeAndFlush(message);
            channelFuture.addListener(future -> {
                if (future.isSuccess())
                    System.out.println(message.getFrom() + " 已经成功发送消息给 " + message.getTo() + " 了。");
            });

        } else {
            /**
             * 否则就直接保存到数据库。。。。
             */
            System.out.println(message.getTo() + " 已经下线了。。。");
        }
    }
}
